package com.hangzhou.xyj.ui.activity;

import android.app.Dialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.GroupPushMsgDao;
import com.hangzhou.xyj.manager.ChatManager;
import com.hangzhou.xyj.model.GroupPushMsgModel;
import com.hangzhou.xyj.model.type.EMsgHandleResultType;
import com.hangzhou.xyj.net.handler.CommonBooleanHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.AgreeJoinGroupReq;
import com.hangzhou.xyj.net.request.RefuseJoinGroupReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.SystemMessageAdapter;
import com.hangzhou.xyj.ui.widget.SelectMenuDialog;
import com.hangzhou.xyj.utils.LogUtil;

import java.util.List;

/**
 * 不适用，使用MyPersonalInfoActivity
 */
public class MySystemMessageActivity extends BaseActivity {
    RecyclerView mRecyclerView;
    private SystemMessageAdapter mAdapter;
    private GroupPushMsgModel mMessageModel; //本页面需要处理的消息
    private int mPosition; //本页面处理的消息所在位置
    private List<GroupPushMsgModel> mAllMessage;

    @Override
    public void initTop() {
        enableTitle(true, "通知");
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_system_message);
    }

    @Override
    public void initViews() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_content);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new SystemMessageAdapter(this, null));
    }

    @Override
    public void initDatas() {
        //置admin会话消息为已读
        ChatManager.getInstance().markAllMessagesAsRead("admin");
        GroupPushMsgDao dao = new GroupPushMsgDao(mContext);
        //更新消息为已读
        dao.updateIsRead(XYJApplication.getUserInfo().getUserId());

        mAllMessage = dao.getAllMessage(XYJApplication.getUserInfo().getUserId());
        if (mAllMessage == null || mAllMessage.isEmpty()) {
            return;
        }
        mAdapter.update(mAllMessage);
    }

    @Override
    public void initListeners() {
        mAdapter.setOnMessageItemClickListener(new SystemMessageAdapter.OnMessageItemClickListener() {
            @Override
            public void onAgreeClick(GroupPushMsgModel model, int position) {
                mMessageModel = model;
                mPosition = position;
                //同意加入
                agreeJoin(model.getPartUserId(), model.getGroupId());
                LogUtil.d(TAG, "同意加入");
            }

            @Override
            public void onRefuseClick(GroupPushMsgModel model, int position) {
                mMessageModel = model;
                mPosition = position;
                //拒绝加入
                refuseJoin(model.getPartUserId(), model.getGroupId());
                LogUtil.d(TAG, "拒绝加入");
            }

            @Override
            public void onItemClick(GroupPushMsgModel model, int position) {
                mMessageModel = model;
                mPosition = position;
                //item点击
                showDialog(model, position);
                LogUtil.d(TAG, "弹出对话框");
            }
        });
    }

    /**
     * 同意加入
     *
     * @param userId
     * @param groupId
     */
    private void agreeJoin(String userId, String groupId) {
        AgreeJoinGroupReq req = new AgreeJoinGroupReq(userId, groupId);
        HttpLoader.getInstance().sendRequest(new CommonBooleanHandler(req, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                //同意加入成功，处理消息为已同意
                if (mMessageModel != null) {
                    mMessageModel.setHandleResult(EMsgHandleResultType.AGREEED);
                    //更新数据库
                    GroupPushMsgDao dao = new GroupPushMsgDao(mContext);
                    dao.add(mMessageModel);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }, 0));
    }

    /**
     * 拒绝加入
     * @param userId
     * @param groupId
     */
    private void refuseJoin(String userId, String groupId) {
        RefuseJoinGroupReq req = new RefuseJoinGroupReq(userId, groupId);
        HttpLoader.getInstance().sendRequest(new CommonBooleanHandler(req, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                //拒绝加入成功
                if (mMessageModel != null) {
                    mMessageModel.setHandleResult(EMsgHandleResultType.REFUSEED);
                    //更新数据库
                    GroupPushMsgDao dao = new GroupPushMsgDao(mContext);
                    dao.add(mMessageModel);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }, 0));
    }

    /**
     * 弹出操作Dialog
     */
    private void showDialog(final GroupPushMsgModel model, final int position) {
        SelectMenuDialog dialog = new SelectMenuDialog(mContext);
        dialog.setMenu(new String[]{"删除"}, new SelectMenuDialog.SelectMenuClickListener() {
            //按钮添加点击事件
            @Override
            public void onSelectMenuClick(Dialog dialog, View item, int pos) {
                switch (pos) {
                    case 0:
                        GroupPushMsgDao dao = new GroupPushMsgDao(mContext);
                        dao.delete(model.getId());
                        if (mAllMessage != null && mAllMessage.size() > pos) {
                            mAllMessage.remove(pos);
                        }
                        mAdapter.notifyItemRemoved(position);
                        break;
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }

}
